package com.leon.common.exception;

import com.leon.model.common.dtos.ResponseResult;
import com.leon.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description:
 * @program: leon-leadnews
 * @author: Leon
 * @date: 2021-03-09 16:16
 **/
@Log4j2
@ControllerAdvice // 控制器增强
public class ExceptionCatch {

    @ExceptionHandler(Exception.class) // 设置拦截的类型
    @ResponseBody
    public ResponseResult exception(Exception exception){
        exception.printStackTrace();
        // 记录日志
        log.error("catch exception:{}",exception.getMessage());
        // 返回设置好的用异常
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

}
